// pages/view/mn/patientsDistribution/patientsDistribution.js
var wxCharts = require('../../../../utils/wxcharts.js');
const network = require('../../../../utils/network.js')
var app = getApp();
var columnChart = null;
var pieChart = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {

    chartTitle_1: '',
    chartTitle_2: '病人分布比例分析',
    cloumn_categories: ['妇产科门诊', '儿保科门诊', '住院妇产科'],
    cloumn_data: [50,43,19],
     

    pie_data: [
      {
        name: '妇产科门诊',
        data: 50
      },
      {
        name: '儿保科门诊',
        data: 43
      },
      {
        name: '住院妇产科',
        data: 19
      }
    ],

    amountList:[],
    totalTimes: ''

  },
  /**
    * 生命周期函数--监听页面加载
    */
  onLoad: function (options) {
    var that = this
    wx.showLoading({
      title: '加载中',
    })
    that.myRequest(options)
   
  },
  /**
 * 调用封装好的网络请求，获取数据
 */
  myRequest: function (options) {
    var that = this
    console.log('options:', options)
    //调用封装好的网络请求，获取数据
    network.get('/mn/patient', {})
      .then(function (res) {
        console.log('res:', res)
        var obj1 = res.data.data.list1
        var obj2 = res.data.data.list2
        var obj3 = res.data.data.list3
        var obj4 = res.data.data.list4
        var obj5 = res.data.data.list5
        that.setData({

          //入库金额最大前10位-柱形图
          cloumn_categories: obj2,
          cloumn_data: obj2,
          pie_data: obj4,
          amountList: obj3,
          totalTimes: obj5.totalTimes
        })

        console.log("amountList", that.data.totalTimes);
      }).then(function (res) {
        wx.hideLoading()
        that.getDate();
        that.createColumnChart();
        that.createPieChart();

      }).catch(function (res) {
        console.log(res)
      })
  },

  getDate: function(){
    var that = this;
    var timestamp = Date.parse(new Date());
    var date = new Date(timestamp);
    //获取年份  
    var Y = date.getFullYear();
    //获取月份  
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    //获取当日日期 
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
    var date= Y + '年' + M + '月' + D + '日'; 
    that.setData({
      chartTitle_1: date+"在院病人分布情况"
    })
  },
  /**
   * 创建 柱形图
   */
  createColumnChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    columnChart = new wxCharts({
      canvasId: 'columnCanvas',
      type: 'column',
      animation: true,
      categories: that.data.cloumn_categories,
      series: [{
        name: '科室',
        data: that.data.cloumn_data,
        format: function (val, name) {

          return val.toFixed(2) + '个';
        }
      }],
      yAxis: {
        format: function (val) {
          return val + '';
        },
        title: '人数',
        min: 0
      },
      xAxis: {
        disableGrid: false,
        type: 'calibration'
      },
      extra: {
        column: {
          width: 15
        }
      },
      width: windowWidth,
      height: 200,
    });
  },

  /**
   * 创建 饼状图
   */
  createPieChart: function (e) {
    var that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    pieChart = new wxCharts({
      animation: true,
      canvasId: 'pieCanvas',
      type: 'pie',
      series: that.data.pie_data,
      width: windowWidth,
      height: 300,
      dataLabel: true,
    });
  }


})